/* cl.ext.comm 0.1*/
(
	function (factory) {
		if (typeof define === 'function' && define.amd) {
			define(['jquery'], factory);
		} else if (typeof module === 'object' && module.exports) {
			module.exports = function( root, jQuery ) {
				if ( jQuery === undefined ) {
					if ( typeof window !== 'undefined' ) {
						jQuery = require('jquery');
					}
					else {
						jQuery = require('jquery')(root);
					}
				}
				factory(jQuery);
			};
		} else {
			factory(jQuery);
		}
	}(
	function ($){
		"use strict";
		var CommonExtModal = function () {	};
		CommonExtModal.prototype= {
			getStringLen: function (str) {
				var realLength = 0;
				var len = str.length;
				var charCode = -1;
				for (var i = 0; i < len; i++) {
					charCode = str.charCodeAt(i);
					if (charCode >= 0 && charCode <= 128) {
						realLength += 1;
					} else {
						realLength += 3;
					}
				}
				return realLength;
			},
			initMergeJson: function (option1, option2) {
				var target = {};
				for (var k in option1) {
					if (option1.hasOwnProperty(k)) {
						target[k] = option1[k]
					}
				}
				for (var m in option2) {
					if (option2.hasOwnProperty(m)) {
						var isUndefined=this.isCheckUndefined(option2[m]);
						if (typeof option2[m] === "object") {
							if (target[m]) {
								if ($.isArray(option2[m])) {
									target[m] = option2[m]
								} else if($.type(option2[m])==="date"){
									target[m] = option2[m];
								}else if(!isUndefined &&option2[m].isValid){
									if(option2[m].isValid()){
										target[m]=option2[m];
									}
								} else{
									if (isUndefined) {
										target[m] = null
									} else {
										target[m] = this.initMergeJson(target[m], option2[m])
									}
								}
							} else {
								target[m] = option2[m]
							}
						} else {
							target[m] = option2[m]
						}
					}
				}
				return target;
			},
			getRemoveJson: function (json, arrRemove) {
				var target = {};
				for (var k in json) {
					if (json.hasOwnProperty(k)) {
						if (arrRemove.indexOf(k) === -1)target[k] = json[k]
					}
				}
				return target;
			},
			getRemoveList: function (list, arrRemove) {
				var newList = [];
				if(!this.isCheckEmpty(list)){
					for (var i=0;i<list.length;i++) {
						newList.push(this.getRemoveJson(list[i],arrRemove));
					}
				}
				return newList;
			},
			getListInfoByPara: function (list, json) {
				if(this.isCheckEmpty(json)) {
					return list;
				}else{
					var templist = [];
					if(!this.isCheckEmpty(list)) {
						for (var i = 0; i < list.length; i++) {
							var info = list[i];
							var b = true;
							for (var k in json) {
								if (k && json.hasOwnProperty(k)) {
									if (info[k].toString() !== json[k].toString()) {
										b = false;
										break;
									}
								}
							}
							if (b) {templist.push(info);}
						}
					}
					return templist;
				}
			},
			getUpdListByJosn:function(list, json){
				for (var i=0;i<list.length;i++) {
					var info=list[i];
					for(var k in json){
						if (json.hasOwnProperty(k)&& info.hasOwnProperty(k)) {
							info[k+"Original"]=info[k];
							info[k]=json[k];
						}
					}
				}
				return list
			},
			getRemoveListByJosn: function (list, json) {
				var ind =-1;
				for (var i=0;i<list.length;i++) {
					var info=list[i];
					var b=true;
					for(var k in json){
						if (json.hasOwnProperty(k)) {
							if (info[k] != json[k]) {
								b = false;
								break;
							}
						}
					}
					if(b){
						ind=i;
						break;
					}
				}
				if(ind>-1){list.splice(ind,1)}
				return list;
			},
			getArrByJsonBool:function(json,arr){
				arr=arr||[];
				var newArr=[];
				for(var k in json){
					if (json.hasOwnProperty(k) && json[k] && arr.indexOf(k)===-1) {
						newArr.push(k)
					}
				}
				return newArr;
			},
			getArrAdd:function(arr,original){
				var newArr=[];
				for(var i=0;i<arr.length;i++){
					var v=arr[i];
					if(original.indexOf(v)===-1 && !this.isCheckEmpty(v)){newArr.push(v)}
				}
				return newArr;
			},
			getArrMinus:function(a1,a2){
				var arrMinus=[];
				for(var i=0;i<a1.length;i++){
					if(a2.indexOf(a1[i])>-1){}
					else{arrMinus.push(a1[i])}
				}
				return this.getArrUnique(arrMinus)
			},
			getArrUnique: function (arr) {
				var result = [], hash = {};
				for (var i = 0, elem; (elem = arr[i]) != null; i++) {
					if (!hash[elem]) {
						result.push(elem);
						hash[elem] = true;
					}
				}
				return result;
			},
			getListUnique: function (list,arrPara) {
				var result = [];
				if($.ExtCommon.isCheckEmpty(arrPara)){
					result=list
				}else{
					var hash = {};
					for (var i = 0;i<list.length; i++) {
						var info=list[i];
						var tempElem=[];
						for(var p=0;p<arrPara.length;p++){
							if(info[arrPara[p]]){tempElem.push(info[arrPara[p]]);}
						}
						var strElem=tempElem.join("|||");
						if (hash[strElem]) {}
						else{
							result.push(info);
							hash[strElem] = true;
						}
					}
				}

				return result;
			},
			getListIntersect:function(list1,list2,id){
				var json1=this.getJsonByGroup(list1,id);
				var json2=this.getJsonByGroup(list2,id);
				var list=[];
				for(var k in json1){
					if (json1.hasOwnProperty(k) && json2.hasOwnProperty(k)) {
						list.push(json1[k][0])
					}
				}
				return list;
			},
			getListClone: function (list) {
				var cloneList=[];
				if(!this.isCheckEmpty(list)){
                    for(var i=0;i<list.length;i++){
                        cloneList.push(this.getJsonClone(list[i]));
                    }
				}
				return cloneList;
			},
			getJsonClone: function (json) {
				var newjson = {};
				for (var k in json) {
					if (json.hasOwnProperty(k)) {
						newjson[k] = json[k];
					}
				}
				return newjson;
			},
			getJsonByGroup: function (list,groupid) {
				var json={};
				for(var i=0;i<list.length;i++){
					var info=this.getJsonClone(list[i]);
					var id=info[groupid].toString();
					if(json[id]){
					}else{
						json[id]=[];
					}
					json[id].push(info);
				}
				return json;
			},
			convertArrByPara: function (list,paraId) {
				paraId=this.isCheckEmpty(paraId)?"id":paraId;
				var arr=[];
				for(var i=0;i<list.length;i++){
					var info=list[i];
					var id=info[paraId];
					if(!this.isCheckEmpty(id)){arr.push(parseInt(id))}
				}
				arr=this.getArrUnique(arr);
				return arr;
			},
			convertListByPara: function (list,para) {
				var newList=[];
				if(!this.isCheckEmpty(list)){
					para=$.type(para)!=="object"?{"text":"name"}:para;
					for (var i = 0; i < list.length; i++){
						var info=this.getJsonClone(list[i]);
						for (var p in para) {
							if (para.hasOwnProperty(p)) {
								var pKey = para[p];
								var value = "";
								var t = $.type(pKey);
								var isKeyUndefined=false;
								switch (t) {
									case "string":
										value = info[pKey];
										isKeyUndefined=this.isCheckUndefined(value);
										value = this.isCheckEmpty(value) ? "" : value;
										break;
									case "object":
										value = info[pKey.key];
										isKeyUndefined=this.isCheckUndefined(value);
										value = this.isCheckEmpty(value,pKey.value)? pKey.defaultValue : value;
										break;
								}
								if (info[p]) {
									info[p + "Original"] = info[p];
									if(isKeyUndefined){
										value=info[p];
									}
								}
								info[p] = value;
							}
						}
						newList.push(info);
					}
				}
				return newList;
			},
			convertJsonByPara:function(json,arrParam){
				if($.ExtCommon.isCheckEmpty(arrParam)){
					return json;
				}else{
					var temp={};
					for (var i = 0; i < arrParam.length; i++){
						var p=arrParam[i];
						if(p.indexOf(":")>-1){
							var arr= p.split(":");
							if(json.hasOwnProperty(arr[0])){
								temp[arr[1]]=json[arr[0]];
							}
						}else{
							if(json.hasOwnProperty(p)){
								temp[p]=json[p];
							}
						}
					}
					return temp;
				}
			},
			getArrByJson: function (json) {
				var newjson = [];
				for (var k in json) {
					if (json.hasOwnProperty(k)) {
						newjson.push(k);
					}
				}
				return newjson;
			},
			getJsonByArr: function (arr) {
				var json = {};
				for(var k=0;k<arr.length;k++){
					json[arr[k].toString()]={};
				}
				return json;
			},
			removeEmpty:function(json,def){
				var newJson={};
				for (var k in json) {
					if (json.hasOwnProperty(k) && !this.isCheckEmpty(json[k],def)) {
						newJson[k]=json[k]
					}
				}
				return newJson;
			},
			getMappingJson:function(json,mapping,isDef){
				var newJson={};
				isDef=this.isCheckUndefined(isDef)?false:isDef;
				if(!this.isCheckUndefined(json)){
					for(var mk in mapping){
						if (mapping.hasOwnProperty(mk)) {
							var mv=mapping[mk];
							var ind=mv.indexOf(".");
							if(ind==-1){
								if(json.hasOwnProperty(mv)){
									if(this.isCheckUndefined(json[mv])){
										if(isDef){newJson[mk]=""}
									}else{
										newJson[mk]=json[mv]
									}
								}else{
									if(isDef){newJson[mk]=""}
								}
							}else{
								var arr=mv.split(".");
								var tempValue=json;
								for(var ia=0;ia<arr.length;ia++){
									if(tempValue.hasOwnProperty(arr[ia])){
										tempValue=tempValue[arr[ia]];
									}
								}
								newJson[mk] = tempValue;
							}
						}
					}
				}
				return newJson;
			},
			convertMappingJson:function(json,mapping){
				var newJson={};
				if(!this.isCheckEmpty(json)){
					for(var m in json){
						if (json.hasOwnProperty(m)) {
							if(mapping.hasOwnProperty(m)){
								if(!$.ExtCommon.isCheckEmpty(mapping[m])){
									newJson[mapping[m]]=json[m];
								}
							}else{
								newJson[m]=json[m];
							}
						}
					}
				}
				return newJson;
			},
			checkListRepeat:function(list,arrKey,interval,msg){
				var repeatWH={};
				var arrErrWHMsg=[];
				for(var i=0;i<list.length;i++){
					var item=list[i];
					var arrValue=[];
					for(var k=0;k<arrKey.length;k++){
						var ak=arrKey[k];
						if(item.hasOwnProperty(ak)){
							var kv=item[ak];
							if(!this.isCheckEmpty(kv)){
								arrValue.push(kv);
							}
						}
					}
					if(arrValue.length>0 && arrValue.length===arrKey.length){
						var ind=(i + 1).toString();
						var key=arrValue.join(interval);
						if(repeatWH.hasOwnProperty(key)){
							repeatWH[key].push(ind);
						}else{
							repeatWH[key]=[ind]
						}
					}
				}
				for(var wh in repeatWH){
					if(repeatWH.hasOwnProperty(wh) && repeatWH[wh].length>1){
						arrErrWHMsg.push(this.getStringConnect(msg,[wh,repeatWH[wh].join(",")]));
					}
				}
				if(arrErrWHMsg.length>0) {arrErrWHMsg.reverse()}
				return arrErrWHMsg;
			},
			checkJsonEmpty:function(data){
				var b=false;
				if(this.isCheckEmpty(data)){
					b=true;
				}else {
					var temp = [];
					for (var k in data) {
						if (data.hasOwnProperty(k)) {
							var strKey = k.toString();
							var c = this.isCheckEmpty(data[strKey]);
							if (temp.indexOf(c) === -1) {
								temp.push(c)
							}
						}
					}
					if (temp.length === 1 && temp[0]) {
						b=true;
					}
				}
				return b;
			},
			checkExistByArr:function(arrMust,arrValue){
				var isExist=true;
				for (var i = 0; i < arrMust.length; i++) {
					if (arrValue.indexOf(arrMust[i]) === -1) {
						isExist = false;
						break
					}
				}
				return isExist;
			},
			isCheckBoolean:function (obj) {
				return typeof obj === "boolean";
			},
			isCheckUndefined:function (obj) {
				return typeof obj === "undefined" || obj == null;
			},
			isCheckEmpty: function (obj,def) {
				var b = this.isCheckUndefined(obj);
				if (!b) {
					var t = $.type(obj);
					switch (t) {
						case "string":
							b = obj.length === 0;
							break;
						case "number":
							b = obj.toString().length === 0;
							break;
						case "array":
							b = obj.length === 0;
							break;
						case "object":
							b = $.isEmptyObject(obj);
							break;
						case "function":
							b=false;
							break;
						default:
							if(obj.length) b = obj.length === 0;
					}
					if(!b&&def){
						b=obj==def
					}
				}
				return b;
			},
			isCheckString: function (obj) {
				return typeof obj === "string";
			},
			isCheckNum: function (value) {
				return $.isNumeric(value);
			},
			getHourInterval: function (ts, te) {
				ts--;
				te = te ? te : ts;
				return this.fillZero(ts) + ":00-" + this.fillZero(te) + ":59";
			},
			fillZero: function (value,len) {
				len=len||2;
				value = value.toString();
				var zero=[];
				for(var i=0;i<len;i++){zero.push("0");}
				var temp = zero.join("") + value;
				return temp.substr(temp.length - len)
			},
			getQueryString:function(name) {
				return this.getQueryStringBase(window.location.search,name);
			},
			getQueryStringBase:function(url,name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = url.substr(url.indexOf("?")+1).match(reg);
				if (r != null) return unescape(r[2]); return null;
			},
			getQueryStringByHash:function(name) {
				return this.getQueryStringBase(window.location.hash,name);
			},
			goLocationHref:function(url){
				setTimeout(function(){
					window.location.href=url;
				},700)
			},
			goHref:function(url){
				window.location.href=url;
			},
			getValueRatio:function(a,b){
				a=this.isCheckEmpty(a)?0:a;
				b=this.isCheckEmpty(b)?0:b;
				var ratio=0;
				if(a>b){
					ratio=1
				}else{
					if(a>0 &&b >0){
						ratio=parseFloat(a/b).toDecimal2();
						ratio=ratio>1?1:ratio
					}
				}
				return parseInt(ratio*100);
			},
			getParentNames: function (list,id,parentid) {
				parentid= this.isCheckEmpty(parentid)?"0":parentid;
				var arrName=this.recursionParent(list,id,parentid);
				arrName.reverse();
				return arrName
			},
			recursionParent:function(list,id,parentid){
				var arrName=[];
				for(var i=0;i<list.length;i++){
					var info =list[i];
					if(info.id==id){
						arrName.push(info.name);
						if(info.parentId!=parentid){
							arrName=arrName.concat(this.recursionParent(list,info.parentId,parentid));
						}
						break;
					}
				}
				return arrName;
			},
			getValueByParagraph:function(text,padleft,data,space) {
                space = $.ExtCommon.isCheckEmpty(space) ? "," : space;
                var strSpan = $.type(data) === "string" ? "<span>" + data + "</span>" : this.getValueBySpan(data, space);
                var strTitle = this.isCheckEmpty(text) ? "" : "<span class='title-absolute'>" + text + "：</span>";
                var strStyle = this.isCheckEmpty(padleft) ? "" : "style='padding-left: " + padleft + "px;'";
                return "<div class='child-inline-block' " + strStyle + ">" + strTitle + strSpan + "</div>";
            },
			getValueBySpan: function (list,space) {
                space = $.ExtCommon.isCheckEmpty(space) ? "," : space;
                return list.length === 0 ? "" : $.map(list, function (info, index) {
                    var str = $.type(info) === "string" ? info : info.name;
                    return "<span>" + str + (index === list.length - 1 ? "" : space) + "</span>";
                }).join("");
            },
			convertWeekHourValue: function (value) {
				var jsonWeekHour={};
				var arrWeekHour=[];
				if($.type(value)==="array"){
					arrWeekHour=value;
					$.each(value, function (i, info) {jsonWeekHour[(i + 1).toString()] = info;});
				}else{
					jsonWeekHour=value;
					for(var wh in value) {
						if (value.hasOwnProperty(wh)) {
							arrWeekHour.push(value[wh]);
						}
					}
				}

				var selectedValue = {};
				for (var w in jsonWeekHour) {
					if (jsonWeekHour.hasOwnProperty(w)) {
						var week = jsonWeekHour[w];
						$.each(week, function (i, h) {
							if (h === 1) {
								if (!selectedValue[w]) {
									selectedValue[w] = []
								}
								selectedValue[w].push(i)
							}
						});
					}
				}

				if($.ExtCommon.isCheckEmpty(selectedValue)){
					return {};
				}else{
					var titleWeek ={"1": "周一", "2": "周二", "3": "周三", "4": "周四", "5": "周五", "6": "周六", "7": "周日"};
					var selectedHtml = [];
					var selectedHtmlSpan = [];
					for (var t in selectedValue) {
						if (selectedValue.hasOwnProperty(t)) {
							var weekName = titleWeek[t];
							var weekValue = selectedValue[t];
							var ts = "-1";
							var te = "-1";
							var arrTimeRange = [];
							for (var u = 0; u < weekValue.length; u++) {
								var wt = weekValue[u];
								if (ts == "-1") ts = wt;
								if (u + 1 < weekValue.length && wt + 1 == weekValue[u + 1]) continue;
								te = wt;
								arrTimeRange.push("<span>" + $.ExtCommon.getHourInterval(ts + 1, te) + (u + 1 == weekValue.length ? "" : ",") + "</span>");
								ts = "-1";
								te = "-1";
							}
							selectedHtml.push('<div class="child-inline-block" style="padding-left: 40px;"><span class="title-absolute">' + weekName + "：</span>" + arrTimeRange.join("") + '</div>')
							selectedHtmlSpan.push('<span>'+weekName+'：</span>'+arrTimeRange.join(""))
						}
					}
					return {arrValue:arrWeekHour,jsonValue: jsonWeekHour, selectedValue: selectedValue, selectedHtml: selectedHtml,selectedHtmlSpan:selectedHtmlSpan};
				}
			},
			getStringConnect:function (str,param) {
				var arr=typeof param==="string"?[param]:param;
				var newStr=str;
				if(!this.isCheckEmpty(arr)){
					newStr=str.replace(/{[^}]+}/g,function($1){
						var ind=parseInt($1.replace("{","").replace("}",""));
						return arr.length>ind?($.ExtCommon.isCheckEmpty(arr[ind])?"":arr[ind]):$1;

					});
				}
				return newStr;
			},
			getUrlNewByParam:function(url,k,v){
				var k1="?"+k+"=";
				var k2="&"+k+"=";
				var str="";
				if(url.indexOf(k1)>-1){
					str=url.replace(k1,k1+v)
				}else if(url.indexOf(k2)>-1){
					str=url.replace(k2,k2+v)
				}else{
					var p=url.indexOf("?")===-1?"?":"&";
					str=url+p+k+"="+v;
				}
				return str;
			},
			getUrlParamByJson:function (json) {
				var arrParam=[];
				for (var k in json) {
					if (json.hasOwnProperty(k)) {
						arrParam.push(k+"="+encodeURIComponent(json[k]));
					}
				}
				return arrParam.join("&")
			},
			getRandom:function(len){
				len=len||3;
				len=len>0?len:1;
				var r=Math.random();
				r=Math.ceil(r*Math.pow(10,len-1));
				var zero=[];
				for(var i=0;i<len;i++){zero.push("0");}
				var strR=r.toString()+zero.join("");
				strR=strR.substr(0,len);
				return strR;
			},
			addCommas :function (x) {
				if (isNaN(x)) {
					return '-';
				}
				x = (x + '').split('.');
				return x[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g,'$1,')
					+ (x.length > 1 ? ('.' + x[1]) : '');
			},
			encodeHTML : function (source) {
				return String(source)
					.replace(/&/g, '&amp;')
					.replace(/</g, '&lt;')
					.replace(/>/g, '&gt;')
					.replace(/"/g, '&quot;')
					.replace(/'/g, '&#39;');
			},
			getCookie:function (type) {
				var data=$.cookie(type);
				return this.isCheckUndefined(data)?{}:$.parseJSON(data);
			},
			setCookie:function (type,data) {
				data=this.isCheckUndefined(data)?{}:data;
				$.cookie(type,JSON.stringify(data));
			},
            getFileExtRegex:function(ext) {
                if(!ext) return null;
                if(typeof ext === 'string') ext = [ext];
                if(ext.length === 0) return null;
                return new RegExp("\.(?:"+ext.join('|')+")$", "i");
            },
            getFileMimeRegex:function(mime) {
                if(!mime) return null;
                if(typeof mime === 'string') mime = [mime];
                if(mime.length === 0) return null;
                return new RegExp("^(?:"+mime.join('|').replace(/\//g, "\\/")+")$", "i");
            },
            conversionEmptyZero:function(json,map){
                $.each(map,function(m,mk){json[mk]= $.ExtCommon.isCheckEmpty(json[mk])?0:json[mk]});
            }
		};
		$.ExtCommon = new CommonExtModal;
		$.ExtCommon.Constructor = CommonExtModal;
	})
);
Number.prototype.toDecimal2=function() {
	if (this) {
		var str=this.toString();
		return str.indexOf(".")>-1?parseFloat(str.substring(0,str.indexOf(".") + 3)):str;
		//return parseFloat(parseInt(this * 100) / 100)
	} else {
		return this;
	}
};

String.prototype.removesplit=function(str) {
	if (this && this.replace) {
		return this.replace(/,/g, '');
	} else {
		return this;
	}
};
Array.prototype.arrclone=function() {
	if (this) {
		return this.slice(0);
	} else {
		return this;
	}
};